package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.DataLabelsComponent;
import com.zygh.policeasset.component.ProjectCode;
import com.zygh.policeasset.mapper.*;
import com.zygh.policeasset.pojo.*;
import com.zygh.policeasset.pojo.vo.ProjectDetailStatus;
import com.zygh.policeasset.pojo.vo.ProjectInfoVO;
import com.zygh.policeasset.pojo.vo.ProjectStatisticsCountVO;
import com.zygh.policeasset.utils.AuditUtil;
import com.zygh.policeasset.utils.EasyExcelUtil;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/ProjectService.class */
public class ProjectService {
    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    TenderMapper tenderMapper;

    @Autowired
    ContractMapper contractMapper;

    @Autowired
    CompleteMapper completeMapper;

    @Autowired
    SettlementMapper settlementMapper;

    @Autowired
    FinalStatementMapper finalStatementMapper;
    @Autowired
    HardwitMapper hardwitMapper;
    @Autowired
    SoftwitMapper softwitMapper;
    @Autowired
    BasicsMapper basicsMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;
    @Autowired
    SectionMapper sectionMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    ProjectCode projectCode;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ProjectNoMapper projectNoMapper;
    @Autowired
    AuditFlowService auditFlowService;
    @Autowired
    DailyProjectMapper dailyProjectMapper;
    @Autowired
    AssetsMapper assetsMapper;

    public List<Project> findAll() {
        return this.projectMapper.findAll();
    }

    public List<ProjectNameType> selectBytype() {
        return this.projectMapper.selectBytype();
    }

    public PageInfo findAllByRoot(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findAllByRoot());
    }

    public PageInfo selectAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.selectAll());
    }

    public List<Project> findAllByRootList() {
        return this.projectMapper.findAllByRoot();
    }

    public PageInfo findChildByRoot(int pid, Integer status, int pageNum, int pageSize, String name, String time1, String time2, String projectNo, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findChildByRoot(pid, status, name, time1, time2, projectNo, userId));
    }

    public Project findById(int id) {
        return this.projectMapper.findById(id);
    }

    public PageInfo findByStatus(int category, String name, int status, String startTime, String endTime, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findByStatus(category, name, status, startTime, endTime));
    }

    public PageInfo search(int category, int status, String name, String commissionNo, int pageNum, int pageSize, String time1, String time2, Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.search(category, status, name, commissionNo, time1, time2, userId));
    }

    public PageInfo findSuperiorProject(int category, int status, String name, String commissionNo, int pageNum, int pageSize, String time1, String time2) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findSuperiorProject(category, status, name, commissionNo, time1, time2));
    }

    public Integer getAdd(Project params) {
        return this.projectMapper.getAdd(params);
    }

    public Integer add(Project project) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        project.setFlowNo(flowNo);
        try {
            if (4 == project.getCategory()) {
                project.setDataLabelsId(DataLabelsComponent.toEnableId);
            }
            String projectNo = getProjectNo(project.getStartDatetime(), Integer.valueOf(project.getCategory()));
            project.setProjectNo(projectNo);
            this.auditFlowService.add(project.getUserId(), flowNo, now, TableType.PROJECT.getName(),
                    TableType.PROJECT.getType(), projectNo, null);
            this.projectMapper.add(project);
            List<UploadFile> uploadFileList = project.getUploadFileList();
            if (!StringUtils.isEmpty(uploadFileList)) {
                for (int i = 0; i < uploadFileList.size(); i++) {
                    uploadFileList.get(i).setProjectId(project.getId().intValue());
                    uploadFileList.get(i).setProjectName("project");
                    uploadFileList.get(i).setCreateDate(DateUtil.now());
                    this.uploadFileMapper.add(uploadFileList.get(i));
                }
            }

            List<UploadFile> invoiceFileList = project.getInvoiceFileList();
            if (!StringUtils.isEmpty(invoiceFileList)) {
                for (int i = 0; i < invoiceFileList.size(); i++) {
                    invoiceFileList.get(i).setProjectId(project.getId().intValue());
                    invoiceFileList.get(i).setProjectName("purchase");
                    invoiceFileList.get(i).setCreateDate(DateUtil.now());
                    invoiceFileList.get(i).setFileType(1);
                    this.uploadFileMapper.add(invoiceFileList.get(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return project.getId();
    }

    public String getProjectNo(String startDatetime, Integer categoryId) {
        Category category = this.categoryMapper.findById(categoryId.intValue());
        String year = startDatetime.substring(0, 4);
        ProjectNo projectNo = this.projectNoMapper.findByType(category.getType(), year);
        if (projectNo == null) {
            projectNo = new ProjectNo();
            projectNo.setType(category.getType());
            projectNo.setCounter(1);
            projectNo.setYear(year);
            this.projectNoMapper.add(projectNo);
        }
        String no = this.projectCode.get(category.getType(), String.valueOf(projectNo.getCounter()), year);
        this.projectNoMapper.update(category.getType(), year);
        if (this.projectMapper.findByNo(no) == null) {
            return no;
        }
        return this.projectCode.get(category.getType(), String.valueOf(projectNo.getCounter() + 1), year);
    }

    public Integer update(Project params) {
        return this.projectMapper.update(params);
    }

    public Integer updateStatus(int id, int status) {
        return this.projectMapper.updateStatus(id, status);
    }

    public Integer remove(int id) {
        int res = 0;
        try {
            res = this.projectMapper.remove(id).intValue();
            this.uploadFileMapper.delete(id, "project");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public PageInfo findbyCompleted(String companyId, String name, String status, String commissionNo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findbyCompleted(companyId, name, status, commissionNo));
    }

    public PageInfo findbySettlement(String companyId, String name, String status, String commissionNo,
                                     int pageNum, int pageSize, Integer dataLabelsId, Integer userId,
                                     String company, Integer id, Double combined) {
        PageHelper.startPage(pageNum, pageSize);
        List<Project> list1 = new ArrayList<>();
        List<Project> list = this.projectMapper.findbySettlement(companyId, name, status, commissionNo, dataLabelsId, userId, company, id);
        if (!StringUtils.isEmpty(combined)) {
            for (Project project : list) {
                Double d = this.assetsMapper.selectCombined(null, null, project.getProjectNo(), null, Integer.valueOf(Integer.parseInt(project.getSectionId())), null);
                if (!StringUtils.isEmpty(d) && new BigDecimal(d.doubleValue()).compareTo(new BigDecimal(combined.doubleValue())) == 0) {
                    list1.add(project);
                }
            }
        } else {
            list1 = list;
        }
        return new PageInfo<>(list1);
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal(0.01d).compareTo(new BigDecimal(0.01d)));
    }

    public Project findbySettlementById(Integer id) {
        return this.projectMapper.findbySettlementById(id);
    }

    public String findByCompanyExcel(String companyId, String name, String status, String commissionNo, Integer dataLabelsId, HttpServletResponse response, Integer userId, String company, Integer id) {
        return this.easyExcelUtil.findByCompanyExcel(response, this.projectMapper.findbySettlement(companyId, name, status, commissionNo, dataLabelsId, userId, company, id));
    }

    public Integer affirmPay(String type, int id, String paymentTime) {
        int res = 0;
        try {
            res = this.projectMapper.affirmPay(id).intValue();
            Project project = this.projectMapper.findById(id);
            if ("zn".equals(type)) {
                this.hardwitMapper.affirmPay(project.getProjectNo(), paymentTime);
                this.softwitMapper.affirmPay(project.getProjectNo(), paymentTime);
            } else if ("jc".equals(type)) {
                this.basicsMapper.affirmPay(project.getProjectNo(), paymentTime);
            } else if ("cg".equals(type)) {
                this.purchaseMapper.affirmPay(project.getProjectNo(), paymentTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public PageInfo getPay(String name, String commissionNo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.getPay(name, commissionNo));
    }

    public PageInfo findAllProjcetName(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.projectMapper.findAllProjcetName());
    }

    public Project findAllProjcetNameByid(int id) {
        return this.projectMapper.findAllProjcetNameByid(id);
    }

    public int updateProjcetNameByid(String name, int id, String projectNo) {
        return this.projectMapper.updateProjcetNameByid(name, id, projectNo);
    }

    public int deleteProjcetNameByid(int id) {
        return this.projectMapper.deleteProjcetNameByid(id);
    }

    public List<Project> toExcel(Integer id) {
        return this.projectMapper.toExcel(id);
    }

    public Integer updateSPD(String projectNo, String startProvisionDate, String certificateNum) {
        return this.projectMapper.updateSPD(projectNo, startProvisionDate, certificateNum);
    }

    public PageInfo selectProject(int pageNum, int pageSize, String projectNo, Integer sectionId, String type) throws InvocationTargetException, IllegalAccessException {
        PageInfo<Project> source = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() ->
                this.projectMapper.selectProject(projectNo, sectionId, type));
        PageInfo<ProjectWholeProcess> target = new PageInfo();
        BeanUtils.copyProperties(source, target);
        List<ProjectWholeProcess> list = getWholeProcess(source.getList());
        target.setList(list);
        return target;
    }

    public CommonResult selectProjectById(Integer id, String type) {
        if ("project".equals(type) || "purchase_assets_business".equals(type)) {
            return new CommonResult().success(projectMapper.findById(id));
        }
        if ("tender".equals(type)) {
            return new CommonResult().success(tenderMapper.findById(id));
        }
        if ("contract".equals(type)) {
            return new CommonResult().success(contractMapper.findById(id));
        }
        if ("completed".equals(type)) {
            return new CommonResult().success(completeMapper.selectById(id));
        }
        if ("settlement".equals(type)) {
            return new CommonResult().success(settlementMapper.selectById(id));
        }
        if ("finalStatement".equals(type)) {
            return new CommonResult().success(finalStatementMapper.findById(id));
        }

        if ("project_assets_business".equals(type)) {
            return new CommonResult().success(settlementMapper.selectById(id));
        }
        return new CommonResult().validateFailed("无此类型");
    }

   /* public List<ProjectWholeProcess> getWholeProcess(List<Project> projectList) {
        List<ProjectWholeProcess> list = new ArrayList<>();
        for (Project project : projectList) {
            List<Section> selectProjectNo = sectionMapper.selectProjectNo(project.getProjectNo());
            if (selectProjectNo.size() > 0) {
                for (Section section : selectProjectNo) {
                    ProjectWholeProcess projectWholeProcess = new ProjectWholeProcess();
                    ProjectStatus projectStatus = projectMapper.selectProjectStatus(project.getProjectNo(), section.getId());
                    projectWholeProcess.setProjectNo(project.getProjectNo());
                    projectWholeProcess.setProjectName(project.getName());
                    projectWholeProcess.setSectionId(section.getId());
                    projectWholeProcess.setSectionName(section.getSectionName());
                    projectWholeProcess.setProjectStatus(projectStatus);
                    projectWholeProcess.setId(project.getId());
                    list.add(projectWholeProcess);
                }
            } else {
                ProjectWholeProcess projectWholeProcess2 = new ProjectWholeProcess();
                projectWholeProcess2.setProjectNo(project.getProjectNo());
                projectWholeProcess2.setId(project.getId());
                projectWholeProcess2.setProjectName(project.getName());
                projectWholeProcess2.setProjectStatus(new ProjectStatus());
                list.add(projectWholeProcess2);
            }
        }
        System.out.println("****************" + list.size());
        return list;
    }*/

    public List<ProjectWholeProcess> getWholeProcess(List<Project> projectList) {
        List<ProjectWholeProcess> list = new ArrayList<>();
        for (Project project : projectList) {
            List<Section> selectProjectNo = sectionMapper.selectProjectNo(project.getProjectNo());
            if (selectProjectNo.size() > 0) {
                for (Section section : selectProjectNo) {
                    ProjectWholeProcess projectWholeProcess = new ProjectWholeProcess();
                    List<ProjectDetailStatus> projectStatus = projectMapper.selectProjectDetailStatus(project.getProjectNo(), section.getId());
                    projectWholeProcess.setProjectNo(project.getProjectNo());
                    projectWholeProcess.setProjectName(project.getName());
                    projectWholeProcess.setSectionId(section.getId());
                    projectWholeProcess.setSectionName(section.getSectionName());
                    projectWholeProcess.setProjectDetailStatusList(projectStatus);
                    projectWholeProcess.setId(project.getId());
                    list.add(projectWholeProcess);
                }
            } else {
                ProjectWholeProcess projectWholeProcess2 = new ProjectWholeProcess();
                List<ProjectDetailStatus> projectStatus = projectMapper.selectProjectDetailStatus(project.getProjectNo(), 0);
                projectWholeProcess2.setProjectNo(project.getProjectNo());
                projectWholeProcess2.setId(project.getId());
                projectWholeProcess2.setProjectName(project.getName());
                projectWholeProcess2.setProjectDetailStatusList(projectStatus);
                list.add(projectWholeProcess2);
            }
        }
        return list;
    }

    public List<BusinessProcessStatus> findBusinessProcess(String projectNo, Integer sectionId) {
        return this.projectMapper.findBusinessProcess(projectNo, sectionId);
    }

    public List<BusinessProcessStatus> findBusinessProcessPurchase(String projectNo, Integer sectionId) {
        return this.projectMapper.findBusinessProcessPurchase(projectNo);
    }

    public List<ProjectInfoVO> selectProject(String type, String name) {
        List<ProjectInfoVO> list = new ArrayList();
        if ("jc".equals(type) || "zn".equals(type)) {
            list = this.projectMapper.getProjectByName(name, type);
        } else if ("da".equals(type)) {
            list = this.dailyProjectMapper.getProjectByName(name);
        } else if ("cg".equals(type)) {
            list = this.purchaseMapper.getProjectByName(name);
        }
        return list;
    }

    public List<Section> findSection(String projectNo) {
        return this.sectionMapper.selectProjectNo(projectNo);
    }

    public List<ProjectStatisticsCountVO> projectStatisticsCount(@Param("status") Integer status,
                                                                 @Param("time1") String time1,
                                                                 @Param("time2") String time2) {
        return this.projectMapper.projectStatisticsCount(status, time1, time2);
    }

    public Integer findPidByCategory(String type) {
        if ("cg".equals(type)) {
            type = "zd";
        }
        return projectMapper.findPidByCategory(type);
    }

}